
// Open the FoxTorrent UI in a new browser window, or focus its window/tab, if it's already open.
function ft_openui()
{
	// This code taken from here: http://developer.mozilla.org/en/docs/Code_snippets:Tabbed_browser
	var url = "chrome://foxtorrent/content/flashloader.html";

	var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
	var browserEnumerator = wm.getEnumerator("navigator:browser");

	// Check each browser instance for our URL
	var found = false;
	while (!found && browserEnumerator.hasMoreElements()){
		var browserInstance = browserEnumerator.getNext().getBrowser();

		// Check each tab of this browser instance
		var numTabs = browserInstance.tabContainer.childNodes.length;
		for(var index = 0; index < numTabs; index++) {
			var currentBrowser = browserInstance.getBrowserAtIndex(index);
			if ("chrome://foxtorrent/content/FoxTorrent2.swf" == currentBrowser.currentURI.spec) {

				// The URL is already opened. Select this tab.
				browserInstance.selectedTab = browserInstance.tabContainer.childNodes[index];

				// Focus *this* browser
				browserInstance.focus();
				found = true;
				break;
			}
		}
	}

	// Our URL isn't open. Open it now.
	if (!found) {
		var recentWindow = wm.getMostRecentWindow("navigator:browser");
		if (recentWindow) {
			// Use an existing browser window
			recentWindow.delayedOpenTab(url, null, null, null, null);
		}
		else {
			// No browser windows are open, so open a new one.
			window.open(url);
		}
	}
}
